package com.example.second;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import static android.provider.SyncStateContract.Helpers.update;

public class LineChartActivity extends AppCompatActivity {

    LinearLayout layout;
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;

    private Timer timer = new Timer();
    int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout = findViewById(R.id.layout_chart);
        inputStream = getResources().openRawResource(R.raw.data);

        openfile();
        deletedata();

        //数据刷新
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        adddate();          //读取数据并添加到数据集
                        getRenderer();      //设置曲线样式及图样式
                        showchart();        //将图添加到布局中
                        break;
                    default:
                        break;
                }
            }
        };

        Timer timer = new Timer("刷新");
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        }, 0, 5000);

        if (count == 30) {
            timer.cancel();
        }

    }

    //设定曲线、数据集、图渲染器
    XYSeries series1 = new XYSeries("  ");
    XYSeries series2 = new XYSeries("  ");
    XYSeries series3 = new XYSeries("  ");
    XYSeries series4 = new XYSeries("  ");
    XYSeries series5 = new XYSeries("  ");
    XYSeries series6 = new XYSeries("  ");
    XYSeries series7 = new XYSeries("  ");
    XYSeries series8 = new XYSeries(" ");
    XYSeries series9 = new XYSeries("  ");
    XYSeries series10 = new XYSeries("  ");
    XYSeries series11 = new XYSeries("  ");
    XYSeries series12 = new XYSeries("  ");
    XYSeries series13 = new XYSeries("  ");
    XYSeries series14 = new XYSeries("  ");
    XYSeries series15 = new XYSeries("  ");
    XYSeries series16 = new XYSeries("  ");
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

    //读取文件的数据
    public void openfile() {
        try {
            inputStreamReader = new InputStreamReader(inputStream, "gbk");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        bufferedReader = new BufferedReader(inputStreamReader);
    }

    //舍去前五行无效数据(包含菌种名、时间及温度)
    public void deletedata() {
        for (int i = 0; i < 5; i++) {
            try {
                bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //读取每一行，分隔数据，转换为float型数据，添加数据到每条曲线，曲线的数据添加到数据集
    public XYMultipleSeriesDataset adddate() {
        String line = "";
        try {
            if ((line = bufferedReader.readLine()) != null) {   //读取每一行
                String[] datas = line.split(",");       //以逗号分隔添加到字符串数组
                for (int i = 1; i <= 16; i++) {
                    Float data = Float.parseFloat(datas[i - 1]);    //每个字符串转换为float
                    System.out.println(i + " , " + data);
                    float addx = count / 12.0f;
                    //通过i判断添加到对应曲线
                    switch (i) {
                        case 1:
                            series1.add(addx, data);
                            break;
                        case 2:
                            series2.add(addx, data);
                            break;
                        case 3:
                            series3.add(addx, data);
                            break;
                        case 4:
                            series4.add(addx, data);
                            break;
                        case 5:
                            series5.add(addx, data);
                            break;
                        case 6:
                            series6.add(addx, data);
                            break;
                        case 7:
                            series7.add(addx, data);
                            break;
                        case 8:
                            series8.add(addx, data);
                            break;
                        case 9:
                            series9.add(addx, data);
                            break;
                        case 10:
                            series10.add(addx, data);
                            break;
                        case 11:
                            series11.add(addx, data);
                            break;
                        case 12:
                            series12.add(addx, data);
                            break;
                        case 13:
                            series13.add(addx, data);
                            break;
                        case 14:
                            series14.add(addx, data);
                            break;
                        case 15:
                            series15.add(addx, data);
                            break;
                        case 16:
                            series16.add(addx, data);
                            break;
                    }
                }
                count++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //曲线添加到数据集
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
        dataset.addSeries(series4);
        dataset.addSeries(series5);
        dataset.addSeries(series6);
        dataset.addSeries(series7);
        dataset.addSeries(series8);
        dataset.addSeries(series9);
        dataset.addSeries(series10);
        dataset.addSeries(series11);
        dataset.addSeries(series12);
        dataset.addSeries(series13);
        dataset.addSeries(series14);
        dataset.addSeries(series15);
        dataset.addSeries(series16);

        return  dataset;
    }

    public XYMultipleSeriesRenderer getRenderer() {

        float width = 0.3f;     //曲线宽度
        //一次设置每条曲线样式并添加到的渲染器中
        XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line1));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line2));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line3));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line4));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line5));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line6));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line7));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line8));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line9));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line10));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line11));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line12));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line13));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line14));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line15));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        xySeriesRenderer = new XYSeriesRenderer();
        xySeriesRenderer.setColor(getResources().getColor(R.color.line16));
        xySeriesRenderer.setLineWidth(width);
        xySeriesRenderer.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer);

        //设置图样式
        renderer.setZoomEnabled(false);                 //不可缩放
        renderer.setPanEnabled(true, false);    //x轴可拉伸，y轴补课拉伸
        renderer.setShowLegend(false);          //不显示图例
        renderer.setMargins(new int[]{20, 40, 20, 10});//上、左、下、右   设置入外边距
        renderer.setShowGrid(true);         //显示网格
        renderer.setGridColor(Color.WHITE);     //网格颜色为白色
        renderer.setApplyBackgroundColor(true);     //允许设置背景
        renderer.setBackgroundColor(Color.BLACK);   //背景为黑色
        renderer.setXTitle("Time/h");               //设置x轴标题
        renderer.setAxisTitleTextSize(20);          //标题字体大小
        renderer.setXLabels(15);
        renderer.setXAxisMin(0.0);
        renderer.setXAxisMax(10.0);
        renderer.setYTitle("Ln/(光度值)");
        renderer.setYLabelsPadding(10);
        renderer.setYLabels(5);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(100);

        return renderer;
    }

    //将曲添加到布局
    public void showchart() {
        GraphicalView view = ChartFactory.getLineChartView(this, dataset, renderer);
        layout.addView(view);
    }
}



